import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QCheckBox, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QSpinBox, QHeaderView, QLabel

class DataReviewTable(QWidget):
    def __init__(self):
        super().__init__()
        self.page_size = 10  # 每页显示的行数
        self.page_number = 1  # 当前页码
        self.total_pages = 1  # 总页数
        self.total_rows = 100  # 总行数
        self.page_label = QLabel(f"第{self.page_number}页, 共{self.total_pages}页", self)
        self.page_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.data = [f"项目名称 {i}" for i in range(1, self.total_rows + 1)]  # 数据集
        self.setupUi()

    def setupUi(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        # 创建 TableWidget，设置 10 行 4 列
        self.tableWidget = QTableWidget(self.page_size, 4, self)
        self.tableWidget.setHorizontalHeaderLabels(["序号", "项目名称", "矿井名称", "状态栏"])

        # 设置列宽
        self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.tableWidget.verticalHeader().setVisible(False)

        # 初始化表格数据
        self.updateTable()

        # 设置行高，并计算总高度
        row_height = 30  # 每一行的高度
        for row in range(self.page_size):
            self.tableWidget.setRowHeight(row, row_height)

        total_height = row_height * self.page_size + self.tableWidget.horizontalHeader().height() + 2  # 2 是额外的边距
        self.tableWidget.setFixedHeight(total_height)

        # 将表格添加到布局中
        layout.addWidget(self.tableWidget)

        # 创建分页控件
        pagination_layout = QHBoxLayout()
        prev_button = QPushButton("上一页", self)
        prev_button.clicked.connect(self.prevPage)
        pagination_layout.addWidget(prev_button)

        pagination_layout.addWidget(self.page_label)

        next_button = QPushButton("下一页", self)
        next_button.clicked.connect(self.nextPage)
        pagination_layout.addWidget(next_button)

        layout.addLayout(pagination_layout)

        # 设置窗口宽度
        self.resize(800, total_height + 50)

    def updateTable(self):
        start_index = (self.page_number - 1) * self.page_size
        end_index = start_index + self.page_size
        data_slice = self.data[start_index:end_index]

        # 填充表格数据，并设置居中对齐
        for row, project_name in enumerate(data_slice):
            # 序号列
            item1 = QTableWidgetItem(f"{start_index + row + 1}")
            item1.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tableWidget.setItem(row, 0, item1)

            # 项目名称列
            item2 = QTableWidgetItem(project_name)
            item2.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tableWidget.setItem(row, 1, item2)

            # 矿井名称列
            item3 = QTableWidgetItem(f"矿井名称 {start_index + row + 1}")
            item3.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tableWidget.setItem(row, 2, item3)

            # 状态栏列（复选框居中）
            checkBoxWidget = QWidget()
            checkBoxLayout = QHBoxLayout(checkBoxWidget)
            checkBoxLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
            checkBoxLayout.setContentsMargins(0, 0, 0, 0)
            checkBox = QCheckBox()
            checkBoxLayout.addWidget(checkBox)
            self.tableWidget.setCellWidget(row, 3, checkBoxWidget)

        self.updatePagination()

    def nextPage(self):
        if self.page_number < self.total_pages:
            self.page_number += 1
            self.updateTable()

    def prevPage(self):
        if self.page_number > 1:
            self.page_number -= 1
            self.updateTable()

    def changePageSize(self, value):
        self.page_size = value
        self.page_number = 1
        self.total_pages = (self.total_rows + self.page_size - 1) // self.page_size
        self.updateTable()

    def updatePagination(self):
        self.total_pages = (self.total_rows + self.page_size - 1) // self.page_size
        self.page_label.setText(f"第{self.page_number}页, 共{self.total_pages}页")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainForm = DataReviewTable()
    mainForm.show()
    sys.exit(app.exec())